package com.jzcn.service.impl;

import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzcn.common.base.BaseServiceImpl;
import com.jzcn.common.dto.enterprise.EnterpriseUserDTO;
import com.jzcn.common.dto.enterprise.EnterpriseUserExcelImportDTO;
import com.jzcn.common.dto.enterprise.SearchEnterpriseUserDTO;
import com.jzcn.common.dto.file.FileDTO;
import com.jzcn.common.dto.log.LogListDTO;
import com.jzcn.common.dto.log.SearchLogDTO;
import com.jzcn.common.dto.system.tool.ToolListDTO;
import com.jzcn.common.exceptions.BusinessException;
import com.jzcn.common.utils.BeanUtils;
import com.jzcn.common.utils.JzPage;
import com.jzcn.common.utils.StringUtil;
import com.jzcn.domain.EntEmployee;
import com.jzcn.domain.SysFile;
import com.jzcn.domain.SysLog;
import com.jzcn.domain.SysResources;
import com.jzcn.mapper.EntEmployeeMapper;
import com.jzcn.mapper.SysLogMapper;
import com.jzcn.service.EntEmployeeService;
import com.jzcn.service.SysFileService;
import com.jzcn.service.SysLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class EntEmployeeServiceImpl extends BaseServiceImpl<EntEmployeeMapper, EntEmployee> implements EntEmployeeService {

    @Resource
    SysFileService sysFileService;

    @Override
    public Boolean userExcelImport(List<EnterpriseUserExcelImportDTO> dto) {
        List<Integer> jobNumberList = super.list().stream().map(EntEmployee::getJobNumber).collect(Collectors.toList());
        List<EntEmployee> EntEmployeeList = new ArrayList<>();
        dto.forEach(r->{
            if(!jobNumberList.contains(Integer.valueOf(r.getJobNumber()))){
                EntEmployee entEmployee = new EntEmployee();
                entEmployee.setJobNumber(Integer.valueOf(r.getJobNumber()));
                entEmployee.setDepartment(r.getDepartment());
                entEmployee.setUserName(r.getUserName());
                entEmployee.setPosition(r.getPosition());
                EntEmployeeList.add(entEmployee);
            }
        });
        //List<EnterpriseUserDTO> enterpriseUser= dto.stream().filter(r-> !jobNumberList.contains(r.getJobNumber())).collect(Collectors.toList());
        //return super.saveBatch(BeanUtils.convertList(enterpriseUser,EntEmployee.class));
        return super.saveBatch(EntEmployeeList);
    }

    @Override
    public JzPage<EnterpriseUserDTO> listPage(SearchEnterpriseUserDTO dto) {
        IPage<EntEmployee> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
//        LambdaQueryWrapper<EntEmployee> wrapper = new LambdaQueryWrapper();
//        if(!StringUtils.isEmpty(dto.getJobNumber())){
//            wrapper.eq(EntEmployee::getJobNumber,dto.getJobNumber());
//        }
//        if(!StringUtils.isEmpty(dto.getKeyword())){
//            wrapper.and(q-> q.like(EntEmployee::getUserName,dto.getKeyword()).or().like(EntEmployee::getRealName,dto.getKeyword()));
//        }
//        //根据排序升序排列，序号越小越在前
//        wrapper.orderByDesc(EntEmployee::getCreateTime);
        return BeanUtils.convertPage(new JzPage<>(baseMapper.findPage(iPage,dto)), EnterpriseUserDTO.class);
    }

    @Override
    public List<EnterpriseUserDTO> dataList(List<Integer> ids) {
        List<EnterpriseUserDTO> entEmployeeList = baseMapper.findIdsList(ids);
        if(!CollectionUtils.isEmpty(entEmployeeList)){
            entEmployeeList.forEach(e->{
                if(e.getFileId() !=null){
                    FileDTO file = sysFileService.getById(e.getFileId());
                    if(Objects.nonNull(file)){
                        e.setFiles(file);
                    }

                }
            });
        }
        return entEmployeeList;
    }

    @Override
    public boolean update(EnterpriseUserDTO dto) {
        dto.setUpdateTime(new Date());
        EntEmployee entEmployee = new EntEmployee();
        BeanUtils.copyProperties(dto,entEmployee);
        return super.update(entEmployee);
    }

    @Override
    public boolean create(EnterpriseUserDTO dto) {
        if(super.checkUniqueness("user_name",dto.getUserName())){
            throw new BusinessException("当前花名已存在");
        }
        dto.setCreateTime(new Date());
        EntEmployee entEmployee = new EntEmployee();
        BeanUtils.copyProperties(dto,entEmployee);
        return super.save(entEmployee);
    }

    @Override
    public boolean delete(Integer id) {
        return super.remove(id);
    }

    @Override
    public EnterpriseUserDTO getByEnterprise(Integer id) {
        EntEmployee entEmployee = super.getById(id);
        EnterpriseUserDTO user = BeanUtils.convert(entEmployee,EnterpriseUserDTO.class);
        if(user.getFileId()!=null){
            user.setFiles(sysFileService.getById(user.getFileId()));
        }
        return user;
    }

}
